package com.morgankelly.action;

import java.util.ArrayList;
import java.util.List;

import com.morgankelly.po.Account;
import com.morgankelly.po.Blog;
import com.morgankelly.util.Constants;

@SuppressWarnings("serial")
public class AccountAction extends BaseAction {
    
	public String input() {

		initAccountForm();
		return INPUT;
	}
	
	public String add() {

	    Account account = new Account();
	    account.setName(name);
	    account.setPassword(password);
	    account.setDisplayName(displayName);
	    account.setEmail(email);
	    account.setRole(role);
	    account.setPicPath(picPath);

	    getAuthService().addAccount(account);
	    
		initAccountForm();
		return ADD;
	}

	public String updateInput() {
		
		Account account;

	    //BlogDAO.update(title, content, accountID, categoryID);
		List<Object> args = new ArrayList<Object>();
		if (accountID == null || accountID == 0) {
            args.add(Blog.class.getSimpleName());
            addActionError(getText(Constants.ERROR_ID_NOT_CHOSEN, args));
            return FAILED;
		} else {
			account = getAuthService().getAccountByID(accountID);
			
			setName(account.getName());
			setPassword(account.getPassword());
			setDisplayName(account.getDisplayName());
			setEmail(account.getEmail());
			setRole(account.getRole());
			setPicPath(account.getPicPath());
		}
	    
		initAccountForm();
		return UPDATE_INPUT;
	}
	
	public String update() {

		getAuthService().updateAccount(accountID, name, password, displayName, email, role, picPath);
	    
		initAccountForm();
		return ADD;
	}
	
	public String delete() {
		getAuthService().deleteAccountByID(accountID);
		
		initAccountForm();
		return DELETE;
	}
	
	private void initAccountForm() {
		setAccountList(getAuthService().getAccountList());
		initSideBar();
	}
	
	private String name;
	private String password;
	private String displayName;
	private String email;
	private String role;
	private String picPath;
	
	private Long accountID;


	public Long getAccountID() {
		return accountID;
	}

	public void setAccountID(Long accountID) {
		this.accountID = accountID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}
}
